package com.yulebaby.callsenter.adapter;

import android.content.Context;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;

import com.yulebaby.callsenter.R;
import com.yulebaby.callsenter.global.App;
import com.yulebaby.callsenter.global.Constant;
import com.yulebaby.callsenter.utils.ComLogUtils;
import com.yulebaby.callsenter.utils.CommonUtil;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * @author YK
 * @date 2017-1-3.
 */

public class ScentMarchAdapter extends BaseAdapter implements ListAdapter {
    //    private ArrayList<String> list;
    private LayoutInflater inflater;
    public MyHolder holder;
    //记录选择的Item
    private HashSet<Integer> positionSet = new HashSet<>();
    private int position;
    private SparseBooleanArray selectedItems = new SparseBooleanArray();//记录选中的position
    private final String[] strings;

    public ScentMarchAdapter(Context context) {
        super();
//        this.list = getList();
        strings = CommonUtil.getStringArray(R.array.scent_item_text);

        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return strings.length;
    }

    @Override
    public Object getItem(int position) {

        return strings[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        this.position = position;
        if (convertView == null) {
            holder = new MyHolder();
            convertView = inflater.inflate(R.layout.item_scentmarch, null);
            holder.tv_sureintent = (TextView) convertView.findViewById(R.id.tv_sureintent);
            holder.iv_sureintent = (ImageView) convertView.findViewById(R.id.iv_sureintent);
            holder.tv_upload_sureintent = (TextView) convertView.findViewById(R.id.tv_upload_sureintent);
            holder.iv_arrow_sureintent = (ImageView) convertView.findViewById(R.id.iv_arrow_sureintent);
            convertView.setTag(holder);
        } else {
            holder = (MyHolder) convertView.getTag();
        }
        holder.tv_sureintent.setText(strings[position]);
        //item选中取消逻辑
        if (isSelected(position)) {
            holder.iv_sureintent.setBackgroundResource(R.drawable.tracerecord_sure);
            positionSet.add(position);
            ComLogUtils.d("1");

        } else {
            holder.iv_sureintent.setBackgroundResource(R.drawable.tracerecord_normal);
            positionSet.remove(position);
            ComLogUtils.d("2");
        }

        return convertView;
    }

    public static class MyHolder {
        public TextView tv_sureintent, tv_upload_sureintent;
        public ImageView iv_sureintent, iv_arrow_sureintent;
    }

    /**
     * 判读是否选中
     */
    public boolean isSelected(int position) {
        return getSelectedItems().contains(position);
    }

    /**
     * 获取选中的items
     */
    public List<Integer> getSelectedItems() {
        List<Integer> items = new ArrayList<>(selectedItems.size());
        for (int i = 0; i < selectedItems.size(); i++) {
            items.add(selectedItems.keyAt(i));
        }
        return items;
    }

    /**
     * 切换选中或取消选中
     */
    public void switchSelectedState(int position) {
        if (selectedItems.get(position, false)) {
            selectedItems.delete(position);
            //再次选中为false
            App.isState = false;
            ComLogUtils.d("非选择；；；"+position);
        } else {
            selectedItems.put(position, true);
            //选中为true
            App.isState = true;
            ComLogUtils.d("选择；；；"+position);
        }
        this.notifyDataSetChanged();
    }


}
